<!-- 呈批件列表 -->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" class="search-form" size="small" :inline="true" v-show="showSearch" label-width="">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="编号" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <!-- 右边浮动按钮 -->
      <div class="right-btn">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:batch:add']"
        >新建呈批件</el-button>
      </div>
    </el-form>
    <!-- @selection-change="handleSelectionChange" -->
    <el-table @row-dblclick="rowDetails" v-loading="loading" :data="tableList" :max-height="height">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="序号" align="center" fixed="left">
        <template slot-scope="scope">
          <span>{{scope.$index+1 + (queryParams.pageNum-1)*queryParams.pageSize}}</span>
        </template>
      </el-table-column>
      <el-table-column label="编号" align="center" prop="number" :show-overflow-tooltip="true" />
      <el-table-column label="标题" align="center" prop="title" min-width="200" :show-overflow-tooltip="true" />
      <el-table-column label="缓急程度" align="center" prop="pressing" min-width="100" :formatter="pressingFormat" :show-overflow-tooltip="true" />
      <el-table-column label="密级" align="center" prop="security" :formatter="securityFormat" :show-overflow-tooltip="true" />
      <el-table-column label="提交部门" align="center" prop="applyDepartmentName" min-width="100" :show-overflow-tooltip="true" />
      <el-table-column label="提交人" align="center" prop="applyUser" :show-overflow-tooltip="true" />
      <el-table-column label="发布时间" align="center" prop="createTimeN" min-width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTimeN) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="state" :formatter="statusFormat" min-width="100" :show-overflow-tooltip="true" />
      <el-table-column label="当前处理人" align="center" prop="nowApply" min-width="110" :show-overflow-tooltip="true" />
      <el-table-column label="当前节点" align="center" prop="nowNode" min-width="100" :show-overflow-tooltip="true" />
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="250px">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.state == 1"
            size="mini"
            type="text"
            @click="handleOpen(scope.row)"
          >启动</el-button>
          <el-button
            v-if="scope.row.isTransaction == 1"
            size="mini"
            type="text"
            @click="handleApproval(scope.row)"
          >处理</el-button>
          <el-button
            v-if="scope.row.isTransaction == 3"
            size="mini"
            type="text"
            @click="handleApproval(scope.row)"
          >承办</el-button>
          <el-button
            v-if="scope.row.isTransaction == 4"
            size="mini"
            type="text"
            @click="handleApproval(scope.row)"
          >留档</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDetails(scope.row)"
          >查看</el-button>
          <el-button
            v-if="scope.row.state == 1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:batch:edit']"
          >编辑</el-button>
          <el-button
            v-if="scope.row.state == 1"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:batch:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listApprovalDoc,startProcessModel,delProcessModel } from "@/api/system/approvalDocManage";
import Editor from '@/components/Editor';
import { getToken } from "@/utils/auth";
import { treeselect } from "@/api/system/statute";

export default {
  name: "ApprovalDocManage",
  components: {
    Editor
  },
  data() {
    return {
      height: (document.documentElement.clientHeight || document.body.clientHeight)-220,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 新闻资讯表格数据
      tableList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      statusOptions: [],
      // 缓急程度数据字典
      degreeOptions: [],
      // 密级数据字典
      secretLevelOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
        number: undefined,
      },
    };
  },
  created() {
    console.log(this.$route)
    this.getList();
    this.getDicts("approval_doc_state").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("degree_urgency").then(response => {
      this.degreeOptions = response.data;
    });
    this.getDicts("secret_level").then(response => {
      this.secretLevelOptions = response.data;
    });
  },
  methods: {
    /** 查询新闻资讯列表 */
    getList() {
      this.loading = false;
      listApprovalDoc(this.queryParams).then(response => {
        console.log(response)
        this.tableList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.informationId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 启动按钮操作 */
    handleOpen(row){
      console.log(row)
      this.$confirm("确认启动该流程吗？", "提示", {}).then(()=>{
        var params = { id:row.id }
        startProcessModel(params).then((res)=>{
          if(res.code == 200){
            this.msgSuccess('启动成功')
            this.getList()
          }
        })
      }).catch(()=>{})
    },
    /** 每行双击事件 */
    rowDetails(row, column, event){
      this.$store.dispatch('tagsView/delView', {name: 'ApprovalDoc'}).then(({ visitedViews }) => {
        this.$router.push({
          name: 'ApprovalDoc',
          params: {
            docId: row.id,
            taskId: row.taskId,
            isApproval: false
          }
        })
      })
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$store.dispatch('tagsView/delView', {name: 'AddEditDoc'}).then(({ visitedViews }) => {
        this.$router.push({
          name: 'AddEditDoc',
          isEdit: false,
        })
      })
    },
    /** 处理按钮操作 */
    handleApproval(row){
      this.$store.dispatch('tagsView/delView', {name: 'ApprovalDoc'}).then(({ visitedViews }) => {
        this.$router.push({
          name: 'ApprovalDoc',
          params: {
            docId: row.id,
            taskId: row.taskId,
            isApproval: true
          }
        })
      })
    },
    /** 查看按钮操作 */
    handleDetails(row){
      this.$store.dispatch('tagsView/delView', {name: 'ApprovalDoc'}).then(({ visitedViews }) => {
        this.$router.push({
          name: 'ApprovalDoc',
          params: {
            docId: row.id,
            taskId: row.taskId,
            isApproval: false
          }
        })
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$store.dispatch('tagsView/delView', {name: 'AddEditDoc'}).then(({ visitedViews }) => {
        this.$router.push({
          name: 'AddEditDoc',
          params:{
            docId: row.id,
            isEdit: true,
          },
        })
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const informationIds = row.id || this.ids;
      var params = { ids:informationIds }
      this.$confirm('是否确认删除标题为"' + row.title + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delProcessModel(params);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有新闻资讯数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportInformation(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.msgSuccess(response.msg);
      this.$alert(response.url, '视频地址', {
        dangerouslyUseHTMLString: true
      });
    },
    // 附件上传成功处理
    annexSuccess(response, file, fileList) {
      this.form.fileList=fileList
    },
    // 附件上传失败处理
    annexRemove(file, fileList) {
      this.form.fileList=fileList
    },
    //缓急程度翻译
    pressingFormat(row, column){
      return this.selectDictLabel(this.degreeOptions, row.pressing);
    },
    //密级翻译
    securityFormat(row, column){
      return this.selectDictLabel(this.secretLevelOptions, row.security);
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.state);
    },
    // 接收人为空判断
    receiveUserFormat(row,colum){
      return row.receiveUserName == null?"所有人":row.receiveUserName;
    },
    // 接收部门为空判断
    receiveDepartmentFormat(row,colum){
      return row.receiveDepartmentName == null?"所有部门":row.receiveDepartmentName;
    },
    //下载
    downloadAttachment(row) {
      if (row.attachmentName != null){
        var attachmentName = row.attachmentName.split(",");
        var attachmentUrl = row.attachmentUrl.split(",");
        for (var k=0;k<attachmentName.length;k++){
          var name = attachmentName[k];
          var url = attachmentUrl[k];
          const a = document.createElement('a');
          a.setAttribute('download', name);
          a.setAttribute('title', name);
          a.setAttribute('target', '_blank');
          a.setAttribute('href', url);
          a.click();
        }
      }
    },
  }
};
</script>
